import {defineStore} from 'pinia'
import {computed, ref} from 'vue'
import {
    createProduct as createProductApi,
    deleteProduct as deleteProductApi,
    getProductDetail as getProductDetailApi,
    getProductList as getProductListApi,
    updateProduct as updateProductApi,
    updateProductStatus as updateProductStatusApi,
    updateProductStock as updateProductStockApi
} from '@/api/product'

/**
 * 商品管理Store
 * 管理商品列表、商品详情、商品状态等数据
 */
export const useProductStore = defineStore('product', () => {
    // 状态
    const products = ref([])
    const currentProduct = ref(null)
    const loading = ref(false)
    const total = ref(0)
    const currentPage = ref(1)
    const pageSize = ref(10)

    // 计算属性
    const productCount = computed(() => products.value.length)
    const hasProducts = computed(() => products.value.length > 0)

    /**
     * 获取商品列表
     * @param {Object} params 查询参数
     */
    const getProductList = async (params = {}) => {
        loading.value = true
        try {
            const response = await getProductListApi({
                current: currentPage.value,
                size: pageSize.value,
                ...params
            })

            if (response.data) {
                products.value = response.data.records || []
                total.value = response.data.total || 0
                currentPage.value = response.data.current || 1
            }

            return response
        } catch (error) {
            console.error('获取商品列表失败:', error)
            throw error
        } finally {
            loading.value = false
        }
    }

    /**
     * 获取商品详情
     * @param {number} id 商品ID
     */
    const getProductDetail = async (id) => {
        loading.value = true
        try {
            const response = await getProductDetailApi(id)
            if (response.data) {
                currentProduct.value = response.data
            }
            return response
        } catch (error) {
            console.error('获取商品详情失败:', error)
            throw error
        } finally {
            loading.value = false
        }
    }

    /**
     * 创建商品
     * @param {Object} productData 商品数据
     */
    const createProduct = async (productData) => {
        loading.value = true
        try {
            const response = await createProductApi(productData)
            // 创建成功后刷新列表
            await getProductList()
            return response
        } catch (error) {
            console.error('创建商品失败:', error)
            throw error
        } finally {
            loading.value = false
        }
    }

    /**
     * 更新商品
     * @param {number} id 商品ID
     * @param {Object} productData 商品数据
     */
    const updateProduct = async (id, productData) => {
        loading.value = true
        try {
            const response = await updateProductApi(id, productData)
            // 更新成功后刷新列表
            await getProductList()
            return response
        } catch (error) {
            console.error('更新商品失败:', error)
            throw error
        } finally {
            loading.value = false
        }
    }

    /**
     * 删除商品
     * @param {number} id 商品ID
     */
    const deleteProduct = async (id) => {
        loading.value = true
        try {
            const response = await deleteProductApi(id)
            // 删除成功后刷新列表
            await getProductList()
            return response
        } catch (error) {
            console.error('删除商品失败:', error)
            throw error
        } finally {
            loading.value = false
        }
    }

    /**
     * 更新商品状态
     * @param {number} id 商品ID
     * @param {number} status 状态值
     */
    const updateProductStatus = async (id, status) => {
        loading.value = true
        try {
            const response = await updateProductStatusApi(id, status)
            // 更新成功后刷新列表
            await getProductList()
            return response
        } catch (error) {
            console.error('更新商品状态失败:', error)
            throw error
        } finally {
            loading.value = false
        }
    }

    /**
     * 更新商品库存
     * @param {number} id 商品ID
     * @param {number} stock 库存数量
     */
    const updateProductStock = async (id, stock) => {
        loading.value = true
        try {
            const response = await updateProductStockApi(id, stock)
            // 更新成功后刷新列表
            await getProductList()
            return response
        } catch (error) {
            console.error('更新商品库存失败:', error)
            throw error
        } finally {
            loading.value = false
        }
    }

    /**
     * 设置分页参数
     * @param {number} page 页码
     * @param {number} size 每页大小
     */
    const setPagination = (page, size) => {
        currentPage.value = page
        pageSize.value = size
    }

    /**
     * 重置状态
     */
    const resetState = () => {
        products.value = []
        currentProduct.value = null
        loading.value = false
        total.value = 0
        currentPage.value = 1
        pageSize.value = 10
    }

    return {
        // 状态
        products,
        currentProduct,
        loading,
        total,
        currentPage,
        pageSize,

        // 计算属性
        productCount,
        hasProducts,

        // 方法
        getProductList,
        getProductDetail,
        createProduct,
        updateProduct,
        deleteProduct,
        updateProductStatus,
        updateProductStock,
        setPagination,
        resetState
    }
})
